import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mode/utils.dart';

/// kiven 2021/8/20
class BottomBarDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _BottomBarDemoState();
  }
}

class _BottomBarDemoState extends State<BottomBarDemo> {
  var style = TabStyle.fixedCircle;
  var isCenterClick = false;
  
  var itemTotal = 5;
  var initialActiveIndex = 2;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('BottomBarDemo'),
        centerTitle: true,
      ),
      body: SingleChildScrollView(child: Wrap(
        spacing: 8,
        runSpacing: 4,
        children: [
          addAction('style', () {
            final vv = TabStyle.values.map((e) => e.toString()).toList();
            showMyBottomSheet(context, vv).then((value) {
              if(value < 0) return;
              final ss = TabStyle.values.firstWhere((element) => element.toString() == vv[value]);
              if ((itemTotal == 4 || itemTotal == 2) && (ss == TabStyle.fixedCircle || ss == TabStyle.fixed)) {
                showToast('选项数量为偶数时，style不能为fixed和fixedCircle');
                return;
              }
              setState(() {
                style = ss;
              });
            });
          }),
          addAction('中间按钮是否可点击', () {
            showMyBottomSheet(context, ['可点击', '不可点击']).then((value) {
              switch (value) {
                case 0:
                  isCenterClick = true;
                  break;
                case 1:
                  isCenterClick = false;
                  break;
              }
            });
          }),
          addAction('菜单数量', () {
            showMyBottomSheet(context, ['5', '4', '3', '2', '1']).then((value) {
              final total = 5 - value;
              if ((total == 4 || total == 2) && (style == TabStyle.fixedCircle || style == TabStyle.fixed)) {
                showToast('style是fixedCircle或fixed，选项数量不能为偶数');
                return;
              }

              setState(() {
                itemTotal = total;
                initialActiveIndex = itemTotal ~/2;
              });
            });
          })
        ],
      ),),
      bottomNavigationBar: ConvexAppBar(
        items: getTabItems(),
        initialActiveIndex: initialActiveIndex,//optional, default as 0
        style: style,
        onTap: (int i) => print('click index=$i'),
        onTabNotify: (position) {
          if (isCenterClick) return true;

          final r = position != initialActiveIndex;
          if (!r) showToast('点我干啥');
          return r;
        },
      ),
    );
  }

  addTitle(String title) {
    return Center(
      child: Text(title),
    );
  }

  addAction(String title, Function() action) {
    return ElevatedButton(
      onPressed: action,
      child: Text(title),
    );
  }
  
  getTabItems() {
    var ti = [TabItem(icon: Icons.home, title: 'Home')];
    if(itemTotal > 4) {
      ti.add(TabItem(icon: Icons.map, title: 'Discovery'));
    }
    if(itemTotal > 3) {
      ti.add(TabItem(icon: Icons.add, title: 'Add'));
    }
    if(itemTotal > 2) {
      ti.add(TabItem(icon: Icons.message, title: 'Message'));
    }
    if(itemTotal > 1) {
      ti.add(TabItem(icon: Icons.people, title: 'Profile'));
    }

    return ti;
  }
}